package com.atguigu.api4

import com.atguigu.api.SensorReading
import org.apache.flink.streaming.api.TimeCharacteristic
import org.apache.flink.streaming.api.scala._
import org.apache.flink.table.api.scala._
import org.apache.flink.table.api.{EnvironmentSettings, Table}
import org.apache.flink.types.Row

/**
 * @description: 从流中获取到table,时间语义为处理时间
 * @time: 2020/7/22 17:22
 * @author: baojinlong
 **/
object TimeAndWindowTest3 {
  def main(args: Array[String]): Unit = {
    val environment: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment
    // 设置并行度
    environment.setParallelism(1)
    environment.setStreamTimeCharacteristic(TimeCharacteristic.ProcessingTime)


    val setting: EnvironmentSettings = EnvironmentSettings.newInstance
      .useBlinkPlanner
      .inStreamingMode
      .build
    val tableEnv: StreamTableEnvironment = StreamTableEnvironment.create(environment, setting)

    // 从文本读取
    val inputStreamFromFile: DataStream[String] = environment.readTextFile("E:/big-data/FlinkTutorial/src/main/resources/sensor.data")
    // 基本转换操作
    val dataStream: DataStream[SensorReading] = inputStreamFromFile
      .map(data => {
        val dataArray: Array[String] = data.split(",")
        SensorReading(dataArray(0), dataArray(1).toLong, dataArray(2).toDouble)
      })

    // 定义处理时间
    val sensorTable: Table = tableEnv.fromDataStream(dataStream, 'id, 'temperature, 'pt.proctime)
    sensorTable.printSchema()
    sensorTable.toAppendStream[Row].print

    environment.execute("time and window test job")
  }

}
